package com.greendragon.web.listener;

import java.util.Date;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.greendragon.control.constant.GlobalConstants;
import com.greendragon.persistence.domain.SiteInfo;
import com.greendragon.service.backend.ISiteInfoBackendService;

/**
 * @author wulibin, exceptsky@gmail.com, 2011-12-1 下午04:54:59
 * 
 */
public class StartupListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent event) {
		System.out.println("Your website is now starting and initializing... ");
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
		/*初始化站点信息*/
		ISiteInfoBackendService siteInfoBackendService = (ISiteInfoBackendService)context.getBean("siteInfoBackendService");
		SiteInfo siteInfo = siteInfoBackendService.get();
		if(null == siteInfo){
			siteInfo = new SiteInfo();
			siteInfo.setTitle("Green Dragon");
			siteInfo.setSubTitle("Have Fun!");
			siteInfo.setArticalAnonymousCommentOn(false);
			siteInfo.setArticalCommentOn(true);
			siteInfo.setProductAnonymousCommentOn(false);
			siteInfo.setProductCommentOn(true);
			siteInfo.setCommentVerify(false);
			siteInfo.setDescription("Green Dragon is a java based web system");
			siteInfo.setKeyWords("green dragon, cms");
			siteInfo.setArticalPageLength(10);
			siteInfo.setProductPageLength(10);
			siteInfo.setFeedPageLength(15);
			siteInfo.setModifiedAt(new Date());
			siteInfoBackendService.add(siteInfo);
		}
		event.getServletContext().setAttribute(GlobalConstants.ATTRIBUTE_SITEINFO, siteInfo);
	}

	@Override
	public void contextDestroyed(ServletContextEvent event) {
		// TODO Auto-generated method stub
	}

}
